package cn.yougou.common;


import lombok.Data;

/**
 * 统一结果返回类
 *
 * @author kup
 * @since 2020-11-30
 */

@Data
public class Responses<T> {

    public Responses() {
    }

    private Responses(ResponseCode responseCode) {
        this.code = responseCode.getCode();
        this.msg = responseCode.getMsg();
    }

    private Responses(ResponseCode responseCode, T result) {
        this.code = responseCode.getCode();
        this.msg = responseCode.getMsg();
        this.result = result;
    }

    private Responses(String code, String msg, T result) {
        this.code = code;
        this.msg = msg;
        this.result = result;
    }


    private String code;//错误码
    private String msg;//消息
    private T result; //数据

    //成功  默认
    public static <T> Responses<T> success() {
        return new Responses(ResponseCode.SUCCESS_CODE);
    }

    //成功 带数据
    public static <T> Responses<T> success(T result) {
        return new Responses(ResponseCode.SUCCESS_CODE, result);
    }

    //失败 只提示错误
    public static <T> Responses<T> error() {
        return new Responses(ResponseCode.ERROR_CODE, null);
    }

    //失败  输出枚举参数
    public static <T> Responses<T> error(ResponseCode code) {
        return new Responses(code, (Object) null);
    }

    //失败 输出自定义错误码信息(不建议使用)
    public static <T> Responses<T> error(String code, String msg) {
        return new Responses(code, msg, (Object) null);
    }

}
